import {
  Address
} from '../../utils/address.js';
import {
  Order
} from '../order/order-model.js';
import {
  My
} from '../my/my-model.js';
import { Base } from '../../utils/base.js';
import { Config } from '../../utils/config.js';
var address = new Address();
var order = new Order();
var my = new My();
var base = new Base();
var config=new Config();
Page({

  data: {
    pageIndex: 1,
    isLoadedAll: false,
    loadingHidden: false,
    orderArr: [],
    addressInfo: null
  },
  onLoad: function() {
    this._loadData();
    this._getAddressInfo();
  },

  onShow: function() {
    //更新订单,相当自动下拉刷新,只有  非第一次打开 “我的”页面，且有新的订单时 才调用。
    var newOrderFlag = order.hasNewOrder();
    if (this.data.loadingHidden && newOrderFlag) {
      this.onPullDownRefresh();
    }
  },

  _loadData: function() {
    var that = this;
    wx.getUserInfo({
      success: res => {
        console.log(res)
        that.setData({
          userInfo: res.userInfo
        })
      }
    })
    this._getOrders();
    order.execSetStorageSync(false); //更新标志位
  },
  getUserInfo: function(e) {
    var userinfo = e.detail.userInfo;
    if (userinfo) {
      this._getOrders;
      this.setData({
        userInfo: e.detail.userInfo,
      })
    }
  },
  /**地址信息**/
  _getAddressInfo: function() {
    var that = this;
    address.getAddress((addressInfo) => {
      that._bindAddressInfo(addressInfo);
    });
  },

  /*修改或者添加地址信息*/
  editAddress: function() {
    var that = this;
    // wx.chooseAddress({
    //     success: function (res) {
    //         var addressInfo = {
    //             name:res.userName,
    //             mobile:res.telNumber,
    //             totalDetail:address.setAddressInfo(res)
    //         };
    //         if(res.telNumber) {
    //             that._bindAddressInfo(addressInfo);
    //             //保存地址
    //             address.submitAddress(res, (flag)=> {
    //                 if (!flag) {
    //                     that.showTips('操作提示', '地址信息更新失败！');
    //                 }
    //             });
    //         }
    //         //模拟器上使用
    //         else{
    //             that.showTips('操作提示', '地址信息更新失败,手机号码信息为空！');
    //         }
    //     }
    // })
    wx.getLocation({
      type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function(res) {
        console.log("定位成功");
        var locationString = res.latitude + "," + res.longitude;
        latitude: res.latitude;
        longitude: res.longitude;
        console.log(locationString);
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1',
          data: {
            "key": "F6KBZ-G2YRX-SIC4K-ZWQD5-TKGB7-GWBIQ",
            "location": locationString
          },
          method: 'GET',
          // header: {}, 
          success: function(res) {
            // success
            console.log("请求成功");
            console.log(res.data);
            console.log("请求数据:" + res.data.result.address);
          },
          fail: function() {
            // fail
            console.log("请求失败");
          },
          complete: function() {
            // complete
            console.log("请求完成");
          }
        })
      },
      fail: function() {
        // fail
        console.log("定位失败");
      },
      complete: function() {
        // complete
        console.log("定位完成");
      }
    })
  },

  /*绑定地址信息*/
  _bindAddressInfo: function(addressInfo) {
    this.setData({
      addressInfo: addressInfo
    });
  },

  /*订单信息*/
  _getOrders: function(callback) {
    var that = this;
    order.getOrders(this.data.pageIndex, (res) => {
      var data = res.data;
      that.setData({
        loadingHidden: true
      });
      if (data.length > 0) {
        that.data.orderArr.push.apply(that.data.orderArr, res.data); //数组合并
        that.setData({
          orderArr: that.data.orderArr
        });
      } else {
        that.data.isLoadedAll = true; //已经全部加载完毕
        that.data.pageIndex = 1;
      }
      callback && callback();
    });
  },

  /*显示订单的具体信息*/
  showOrderDetailInfo: function(event) {
    var id = order.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../order/order?from=order&id=' + id
    });
  },
  
  /*未支付订单再次支付*/
  rePay: function(event) {
    var id = order.getDataSet(event, 'id'),
      index = order.getDataSet(event, 'index');

    //online 上线实例，屏蔽支付功能
    if (order.onPay) {
      this._execPay(id, index);
    } else {
      this.showTips('支付提示', '本产品仅用于演示，支付系统已屏蔽');
    }
  },
  delPay:function(event) {
    var id = order.getDataSet(event, 'id'),
      index = order.getDataSet(event, 'index');
    console.log(id)
    var param = {
      url: 'order/del_order',
      type: 'post',
      data: id,
      sCallback: function (res) {
        callback && callback(true, res);
      }, eCallback(res) {
        callback && callback(false, res);
      }
    };
    // this.request(param);
    base.request(param);
  },
  /*支付*/
  _execPay: function(id, index) {
    var that = this;
    order.execPay(id, (statusCode) => {
      if (statusCode > 0) {
        var flag = statusCode == 2;

        //更新订单显示状态
        if (flag) {
          that.data.orderArr[index].status = 2;
          that.setData({
            orderArr: that.data.orderArr
          });
        }

        //跳转到 成功页面
        wx.navigateTo({
          url: '../pay-result/pay-result?id=' + id + '&flag=' + flag + '&from=my'
        });
      } else {
        that.showTips('支付失败', '商品已下架或库存不足');
      }
    });
  },

  /*下拉刷新页面*/
  onPullDownRefresh: function() {
    var that = this;
    this.data.orderArr = []; //订单初始化
    this._getOrders(() => {
      that.data.isLoadedAll = false; //是否加载完全
      that.data.pageIndex = 1;
      wx.stopPullDownRefresh();
      order.execSetStorageSync(false); //更新标志位
    });
  },


  onReachBottom: function() {
    if (!this.data.isLoadedAll) {
      this.data.pageIndex++;
      this._getOrders();
    }
  },

  /*
   * 提示窗口
   * params:
   * title - {string}标题
   * content - {string}内容
   * flag - {bool}是否跳转到 "我的页面"
   */
  showTips: function(title, content) {
    wx.showModal({
      title: title,
      content: content,
      showCancel: false,
      success: function(res) {

      }
    });
  },

})